package com.neusoft.ecommerce.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.neusoft.ecommerce.dbhelper.LayuiPager;
import com.neusoft.ecommerce.entity.Goods;
import com.neusoft.ecommerce.entity.Stock;
import com.neusoft.ecommerce.service.GoodsService;
import com.neusoft.ecommerce.service.StockService;


/**
 * 
 * @author Administrator
 *
 */
@RestController
public class StockController {
	
	private final StockService stockService;

	private final GoodsService goodsService;


	
	public StockController(StockService stockService, GoodsService goodsService) {
		this.stockService = stockService;
		this.goodsService = goodsService;
	}
	
	@GetMapping("/stock_page")
    public ModelAndView stockPage() {
        return new ModelAndView("stock/stock-list");
    }
	
    @RequestMapping("/stock/list")
    @ResponseBody
    public LayuiPager<Stock> list(@RequestParam(value = "page", defaultValue = "1") String page,
                                    @RequestParam(value = "limit", defaultValue = "10") String limit) {
        System.out.println(page + "" + limit);
        LayuiPager<Stock> pager = new LayuiPager<>(Integer.parseInt(page), Integer.parseInt(limit));
        int count = stockService.findTotal(pager);
        List<Stock> list = stockService.findList(pager);
        for (Stock stock : list) {
            System.out.println(stock);
        }
        pager.setData(list);
        pager.setCount(count);
        pager.setCode(0);
        pager.setMsg("");
        return pager;
    }
	
	@GetMapping("/addstock")
    public ModelAndView addstock() {
        return new ModelAndView("stock/stock-add");
    }
	
	@RequestMapping("/stock/goodsList")
    public List<Goods> findGoodsList() {
	    return goodsService.findAll();
    }
	
	@PostMapping("/stock/add")
    public Map<String, Object> add(Stock stock) {
        System.out.println(stock);
        Map<String, Object> map = new HashMap<>(16);
        if (stockService.add(stock)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	
	@GetMapping("/stock/edit_page")
    public ModelAndView editPage(Stock stock, Model model) {
        System.out.println(stock);
        LayuiPager<Stock> pager = new LayuiPager<>(1,1);
        pager.setCondition(stock);
        Stock message = stockService.findList(pager).get(0);
        System.out.println(message);
        ModelAndView view = new ModelAndView("stock/stock-edit");
        model.addAttribute("stock", message);
        return view;
    }
	

	@PostMapping("/stock/edit")
    public Map<String, Object> edit(Stock stock) {
        System.out.println(stock);
        Map<String, Object> map = new HashMap<>(16);
        if (stockService.edit(stock)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	@PostMapping("/stock/delete")
    public Map<String, Object> delete(Stock stock) {
        System.out.println(stock);
        Map<String, Object> map = new HashMap<>(16);
        if (stockService.delete(stock)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }

}
